<?php

class Application_Model_Session extends Zend_Db_Table_Abstract
{
    protected $_name = "tabel_session";
    
    public function insertSession($data)
    {
        $this->insert($data);
    }
    
    public function deleteSession($namespace)
    {
        $result = $this->checkNamespaceSession($namespace);
        if($result)
        {
            foreach ($result as $session){
                $this->delete('id = '.$session->id);
            }
        }
        else
            return false;
    }
    
    public function deleteSessionById($id)
    {
        $this->delete('id_namespace = '.$id);
    }
    
    public function updateSession($data, $id)
    {
        $this->update($data, 'id ='.$id);
    }

    public function getSession($namespace)
    {
        $select = $this->select();
        $select->from($this->_name)
               ->where('namespace = ?',$namespace);
        
        $result = $this->fetchAll($select);
        $result->toArray();
        
        if($result)
            return $result;
        else
            return false;
    }
    
    public function checkSession($id_namespace, $namespace)
    {
        $select = $this->select();
        $select->from($this->_name)
               ->where('namespace = ?', $namespace)
               ->where('id_namespace = ?', $id_namespace);
        
        $result = $this->fetchAll($select);
        $row = $result->count();
        
        if($row > 0)
            return false;
        else
            return true;
    }
    
    public function checkNamespaceSession($namespace)
    {
        $select = $this->select();
        $select->from($this->_name)
               ->where('namespace = ?', $namespace);
        
        $result = $this->fetchAll($select);
        $row = $result->count();
        
        if($row > 0)
            return $result;
        else
            return false;
    }
    
    public function getDataJoin($tabel_name, $join_anchor, $namespace, $tabel_2 = null, $join_anchor2 = null)
    {
        $select = $this->select();
        $select->setIntegrityCheck(false)
               ->from($this->_name)
               ->where('namespace = ?', $namespace)
               ->joinLeft($tabel_name, 'tabel_session.id_namespace = '.$join_anchor);
        
        if($tabel_2 != null and $join_anchor2 != null)
        {
            $select->joinLeft($tabel_2, $join_anchor2);
        }
        
        $result = $this->fetchAll($select)->toArray();
        return $result;
    }

}

